using AutoMapper;
using Core.SimpleTemp.Application;
using Core.SimpleTemp.Application.UserApp;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.SimpleTemp.Repositories.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Repositories;
using Core.Toolkit.Message;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Mvc.Api.sys
{
    /// <summary>
    /// FL_Evaluation
    ///</summary>
    [ApiExplorerSettings(GroupName = "Sys")]//swagger分类标签
    [Route("api/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    public class FL_EvaluationController : CoreApiController
    {
        private readonly CoreDBContext _coreDBContext;
        private readonly FL_EvaluationService _service;
        private readonly FL_StandardInformationService _Infservice;
        private readonly SysUserAppService _Userservice;
        private readonly FL_EvaluationNotConformService _Evaluationservice;
        private readonly WrkerRelationsService _wrkerRelationsService;
        private readonly IMessage _message;
        SQLHelper _sQLHelper;
        /// <summary>
        /// FL_Evaluation
        ///</summary>
        public FL_EvaluationController(FL_EvaluationService service, CoreDBContext coreDBContext, FL_EvaluationNotConformService Evaluationservice, SysUserAppService Userservice, IMessage message, WrkerRelationsService wrkerRelationsService, FL_StandardInformationService Infservice, SQLHelper sQLHelper)
        {
            _Evaluationservice = Evaluationservice;
            _service = service;
            _Infservice = Infservice;
            _coreDBContext = coreDBContext;
            _Userservice = Userservice;
            _message = message;
            _wrkerRelationsService = wrkerRelationsService;
            _sQLHelper = sQLHelper;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetList")]
        //[PermissionFilter(nameof(FL_EvaluationController) + "_" + nameof(FL_EvaluationController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetList(PagingModel<FL_EvaluationDto> pagingQueryModel)
        {
            ////工厂
            //  pagingQueryModel.Model.CreateFactory = UserIdentity.Factory;


            IPageModel<FL_EvaluationDto>
                result = await _service.GetList(pagingQueryModel);

            //result.PageData.ForEach(dto =>
            //{
            //    var str = "";
            //    //dto.FL_EvaluationDept.ToList().ForEach(a =>
            //    //{
            //    //    str += _coreDBContext.SysDepartment.Where(x => x.Id == a.DeptId).FirstOrDefault()?.Name + ",";
            //    //});
            //    if (str.Length > 0)
            //        str = str.Substring(0, str.Length - 1);
            //    dto.Mark03 = str;

            //});
            return this.JsonSuccess(result);
        }

        [HttpPost("GetDepList")]
        //[PermissionFilter(nameof(FL_EvaluationController) + "_" + nameof(FL_EvaluationController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetDepList(PagingModel<FL_StandardInformationDto> pagingQueryModel)
        {
            ////工厂
            pagingQueryModel.Model.CreateFactory = UserIdentity.Factory;


            IPageModel<FL_StandardInformationDto>
                result = await _Infservice.GetList(pagingQueryModel);

            var list = new List<FL_StandardInformationDto>();

            result.PageData.ForEach(dto =>
            {
                dto.FL_StandardInformationDept1 = dto.FL_StandardInformations.Where(x => x.DeptType == "1").ToList();

                var str = "";
                dto.FL_StandardInformationDept1.ToList().ForEach(a =>
                {
                    str += _coreDBContext.SysDepartment.Where(x => x.Id == a.Class).FirstOrDefault()?.Name + ",";
                });
                if (str.Length > 0)
                    str = str.Substring(0, str.Length - 1);
                dto.Mark03 = str;

                dto.GKBM = dto.FL_StandardInformationDept1.FirstOrDefault().Class;
                //  dto.SysUser= dto.SysUser.Where(x=>x.Id==_coreDBContext.SysUserDuty)
                var Person = _coreDBContext.SysUserDuty.Where(y => y.SysDepartmentID == dto.FL_StandardInformationDept1.FirstOrDefault().Class).ToList();

                List<SysUserDto> ListUser = new List<SysUserDto>();
                for (int i = 0; i < Person.Count; i++)
                {
                    var SysUser = new SysUserDto();
                    SysUser.Id = Person[i].SysUserId;
                    SysUser.Name = _coreDBContext.SysUser.Where(q => q.Id == Person[i].SysUserId).FirstOrDefault().Name;
                    bool Userisnot = true;
                    ListUser.ForEach(a =>
                    {
                        if (a.Name == SysUser.Name)
                        {
                            Userisnot = false;
                        }
                    });
                    if (Userisnot)
                    {
                        ListUser.Add(SysUser);
                    }
                }
                dto.SysUser = ListUser;
             


                bool isnot = true;
                if (list.Count > 0)
                {
                    list.ForEach(a =>
                    {
                        if (a.Mark03 == dto.Mark03)
                        {
                            isnot = false;
                        }
                    });
                }
                if (isnot)
                {
                    list.Add(dto);
                }
            });

            result.PageData = list;
            result.RowCount = list.Count;
            return this.JsonSuccess(result);
        }



        [HttpPost("GetDepEditList")]
        //[PermissionFilter(nameof(FL_EvaluationController) + "_" + nameof(FL_EvaluationController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetDepEditList(PagingModel<FL_StandardInformationDto> pagingQueryModel)
        {
            ////工厂
            pagingQueryModel.Model.CreateFactory = UserIdentity.Factory;
            var Years = pagingQueryModel.Model.Years;

            IPageModel<FL_StandardInformationDto>
                result = await _Infservice.GetList(pagingQueryModel);

            var list = new List<FL_StandardInformationDto>();

            result.PageData.ForEach(dto =>
            {
                dto.FL_StandardInformationDept1 = dto.FL_StandardInformations.Where(x => x.DeptType == "1").ToList();
                dto.FL_StandardInformationReview = dto.FL_StandardInformationReview.Where(x => x.Year == int.Parse(Years)).ToList();
                if (dto.FL_StandardInformationDept1.Count>0)
                {
                    var str = "";
                    dto.FL_StandardInformationDept1.ToList().ForEach(a =>
                    {
                        str += _coreDBContext.SysDepartment.Where(x => x.Id == a.Class).FirstOrDefault()?.Name + ",";
                    });
                    if (str.Length > 0)
                        str = str.Substring(0, str.Length - 1);
                    dto.Mark03 = str;

                    dto.GKBM = dto.FL_StandardInformationDept1.FirstOrDefault().Class;
                    //  dto.SysUser= dto.SysUser.Where(x=>x.Id==_coreDBContext.SysUserDuty)
                    var Person = _coreDBContext.SysUserDuty.Where(y => y.SysDepartmentID == dto.FL_StandardInformationDept1.FirstOrDefault().Class).ToList();

                    List<SysUserDto> ListUser = new List<SysUserDto>();
                    for (int i = 0; i < Person.Count; i++)
                    {
                        var SysUser = new SysUserDto();
                        SysUser.Id = Person[i].SysUserId;
                        SysUser.Name = _coreDBContext.SysUser.Where(q => q.Id == Person[i].SysUserId).FirstOrDefault().Name;
                        bool Userisnot = true;
                        ListUser.ForEach(a =>
                        {
                            if (a.Name == SysUser.Name)
                            {
                                Userisnot = false;
                            }
                        });
                        if (Userisnot)
                        {
                            ListUser.Add(SysUser);
                        }
                    }
                    dto.SysUser = ListUser;
                    if(dto.FL_StandardInformationReview.ToList().Count>0)
                    {
                        var ReviewPerson = dto.FL_StandardInformationReview.Where(x => x.Year == int.Parse(Years)).FirstOrDefault().CAM_ReviewPerson;
                        dto.Mark06 = _coreDBContext.SysUser.Where(x => x.Id == ReviewPerson).FirstOrDefault().Name;
                    }

                    bool isnot = true;
                    if (list.Count > 0)
                    {
                        list.ForEach(a =>
                        {
                            if (a.Mark03 == dto.Mark03)
                            {
                                isnot = false;
                            }
                        });
                    }
                    if (isnot)
                    {
                        if(dto.FL_StandardInformationReview.Where(x=>x.CAM_ReviewPerson !=null).ToList().Count>0)
                        {
                            list.Add(dto);
                        }
                      
                    }
                }
               
            });
            for (int i = 0; i < list.Count; i++)
            {
                int DepCount = 0;
                int CamCount = 0;
                result.PageData.ForEach(dto =>
                {
                    if(dto.FL_StandardInformationDept1.Count>0)
                    {
                        if (dto.FL_StandardInformationDept1.FirstOrDefault().Class == list[i].FL_StandardInformationDept1.FirstOrDefault().Class)
                        {
                            DepCount++;
                        }

                        var Review = dto.FL_StandardInformationReview.Where(x => x.Year == int.Parse(Years) && x.CAM_Type == "评价报告" && x.CAM_Dept == list[i].FL_StandardInformationDept1.FirstOrDefault().Class).ToList();
                      if(Review.Count > 0)
                        {
                            if ((Review[0].FitSituation != null && Review[0].FitSituation != "") || (Review[0].FitSituationOrNotDescribe != null && Review[0].FitSituationOrNotDescribe != ""))
                            {
                                CamCount++;
                            }
                        }
                      
                    }
                });
                list[i].Mark04 = DepCount.ToString();
                list[i].Mark05 = CamCount.ToString();

            }

            result.PageData = list;
            result.RowCount = list.Count;
            return this.JsonSuccess(result);
        }


        [HttpPost("GetDepPSEditList")]
        //[PermissionFilter(nameof(FL_EvaluationController) + "_" + nameof(FL_EvaluationController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetDepPSEditList(PagingModel<FL_StandardInformationDto> pagingQueryModel)
        {
            ////工厂
            pagingQueryModel.Model.CreateFactory = UserIdentity.Factory;
            var Years = pagingQueryModel.Model.Years;

            IPageModel<FL_StandardInformationDto>
                result = await _Infservice.GetList(pagingQueryModel);

            var list = new List<FL_StandardInformationDto>();

            result.PageData.ForEach(dto =>
            {
                dto.FL_StandardInformationDept1 = dto.FL_StandardInformations.Where(x => x.DeptType == "1").ToList();
                dto.FL_StandardInformationReview = dto.FL_StandardInformationReview.Where(x => x.Year == int.Parse(Years)).ToList();
                if (dto.FL_StandardInformationDept1.Count > 0)
                {
                    var str = "";
                    dto.FL_StandardInformationDept1.ToList().ForEach(a =>
                    {
                        str += _coreDBContext.SysDepartment.Where(x => x.Id == a.Class).FirstOrDefault()?.Name + ",";
                    });
                    if (str.Length > 0)
                        str = str.Substring(0, str.Length - 1);
                    dto.Mark03 = str;

                    dto.GKBM = dto.FL_StandardInformationDept1.FirstOrDefault().Class;
                    //  dto.SysUser= dto.SysUser.Where(x=>x.Id==_coreDBContext.SysUserDuty)
                    var Person = _coreDBContext.SysUserDuty.Where(y => y.SysDepartmentID == dto.FL_StandardInformationDept1.FirstOrDefault().Class).ToList();

                    if (dto.FL_StandardInformationReview.ToList().Count > 0)
                    {
                        dto.Mark06 = _coreDBContext.SysUser.Where(x => x.Id == dto.FL_StandardInformationReview.FirstOrDefault().CAM_ReviewPerson).FirstOrDefault().Name;
                    }

                    dto.Mark02 = _coreDBContext.FL_FileLevel.Where(x => x.Id == dto.FileLeve).FirstOrDefault().FileLeve;
                    if (dto.FL_StandardInformationReview.ToList().Count > 0)
                    {
                        if(dto.FL_StandardInformationReview.FirstOrDefault().FitSituationOrNotDescribe==null || dto.FL_StandardInformationReview.FirstOrDefault().FitSituationOrNotDescribe =="")
                        {
                            dto.FL_StandardInformationReview.FirstOrDefault().Mark01 = "符合";
                            dto.FL_StandardInformationReview.FirstOrDefault().Mark02 = dto.FL_StandardInformationReview.FirstOrDefault().FitSituation;
                            dto.Mark04 = "符合";
                            dto.Mark05= dto.FL_StandardInformationReview.FirstOrDefault().FitSituation;
                        }
                        else
                        {
                            dto.FL_StandardInformationReview.FirstOrDefault().Mark01 = "不符合";
                            dto.FL_StandardInformationReview.FirstOrDefault().Mark02 = dto.FL_StandardInformationReview.FirstOrDefault().FitSituationOrNotDescribe;
                            dto.Mark04 = "不符合";
                            dto.Mark05= dto.FL_StandardInformationReview.FirstOrDefault().FitSituationOrNotDescribe;
                        }
                       
                       

                        if (dto.FL_StandardInformationReview.FirstOrDefault().CAM_ReviewPerson == UserIdentity.Id)
                        {
                            list.Add(dto);
                        }
                    }
                }

            });
            result.PageData = list;
            result.RowCount = list.Count;

            return this.JsonSuccess(result);
        }

        /// <summary>
        /// 查询---人员
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetUserList")]
        //[PermissionFilter(nameof(FL_EvaluationController) + "_" + nameof(FL_EvaluationController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetUserList(PagingModel<SysDepartmentDto> pagingQueryModel)
        {
            //IPageModel<SysUserDto>
            //    result = await _Userservice.GetList(pagingQueryModel);
            List<SysUserDto> ListUser = new List<SysUserDto>();
            for (int i = 0; i < _coreDBContext.SysUser.ToList().Count; i++)
            {
                var SysUser = new SysUserDto();
                SysUser.Id = _coreDBContext.SysUser.ToList()[i].Id;
                SysUser.Name = _coreDBContext.SysUser.ToList()[i].Name;
                ListUser.Add(SysUser);
            }

            return this.JsonSuccess(ListUser);
        }


        /// <summary>
        /// 法律法规_合规性评价不符合项
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetEvaluationList")]
        //[PermissionFilter(nameof(FL_EvaluationController) + "_" + nameof(FL_EvaluationController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetEvaluationList(PagingModel<FL_EvaluationNotConformDto> pagingQueryModel)
        {

            IPageModel<FL_EvaluationNotConformDto>
                result = await _Evaluationservice.GetList(pagingQueryModel);
            var NewModel = new FL_EvaluationNotConformDto();
            List<FL_EvaluationNotConformDto> List = new List<FL_EvaluationNotConformDto>();
            int RowCount = 1;
            result.PageData.ForEach(dto =>
            {
                NewModel.notFitItem = dto.notFitItem;
                NewModel.homologousLawRegulation = dto.homologousLawRegulation;
                NewModel.reasonAnalysis = dto.reasonAnalysis;
                NewModel.reformMeasures = dto.reformMeasures;
                NewModel.responsibilityer = dto.responsibilityer;
                NewModel.Mark02 = _coreDBContext.SysUser.Where(x => x.Id == dto.responsibilityer).FirstOrDefault().Name;
                NewModel.finishTime = dto.finishTime;
                NewModel.Mark01 = RowCount.ToString();
                List.Add(NewModel);
                RowCount++;
            });

            return this.JsonSuccess(List);
        }

        /// <summary>
        /// 添加
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(FL_EvaluationController) + "_" + nameof(FL_EvaluationController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            SaveAsync(FL_EvaluationDto dto)
        {
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                var CheckEvaluation = _coreDBContext.FL_Evaluation.Where(x => x.years == dto.years && x.IsDeleted == false).ToList();
                if (CheckEvaluation.Count > 0)
                {
                    return this.JsonFaild("您选择的年份已有合规性评价,无法添加!");
                }

                int checkRev = 0;
                for (int i = 0; i < dto.FL_StandardInformationReview.Count; i++)
                {
                    if(dto.FL_StandardInformationReview.ToList()[i].CAM_ReviewPerson ==null)
                    {
                        checkRev = 1;
                        break;
                    }
                }
                if(checkRev==1)
                {
                    return this.JsonFaild("您选择评价人员!");
                }

                ////工厂
                dto.CreateFactory = UserIdentity.Factory;
                dto.CreateTime = System.DateTime.Now;
                dto.IsDeleted = false;
                dto.CreateUser = UserIdentity.Id;
                dto.State = "保存";

                await _service.InsertAsync(dto);


                //从清单插入标准库符合性评审
                for (int i = 0; i < dto.FL_StandardInformationReview.Count; i++)
                {
                    var LawDep = _coreDBContext.FL_StandardInformationDept.Where(x => x.Class == dto.FL_StandardInformationReview.ToList()[i].CAM_Dept && x.DeptType == "1" && x.IsDeleted == false).ToList();
                    for (int j = 0; j < LawDep.Count; j++)
                    {
                        var LawId = LawDep[j].LawRegulationsName;

                        if (_coreDBContext.FL_StandardInformation.Where(x => x.Id == LawId
                        && x.CreateFactory == UserIdentity.Factory && x.IsDeleted == false).ToList().Count > 0)
                        {
                            var FL_StandardInformationReview = new FL_StandardInformationReview();
                            FL_StandardInformationReview.StandardInformationID = LawId;
                            FL_StandardInformationReview.CAM_Type = "评价报告";

                            FL_StandardInformationReview.CAM_Dept =
                            dto.FL_StandardInformationReview.ToList()[i].CAM_Dept;

                            FL_StandardInformationReview.CAM_ReviewPerson = dto.FL_StandardInformationReview.ToList()[i].CAM_ReviewPerson;

                            FL_StandardInformationReview.Year = dto.years;
                            FL_StandardInformationReview.Id = Guid.NewGuid();

                            await _coreDBContext.AddRangeAsync(FL_StandardInformationReview);
                            await _coreDBContext.SaveChangesAsync();
                        }
                    }
                }
               
            }
            return this.JsonSuccess(string.Empty);
        }



        /// <summary>
        /// 修改--保存
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("EditSave")]
        [PermissionFilter(nameof(FL_EvaluationController) + "_" + nameof(FL_EvaluationController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            EditSaveAsync(FL_EvaluationDto dto)
        {
            int checkRev = 0;
            for (int i = 0; i < dto.FL_StandardInformationReview.Count; i++)
            {
                if (dto.FL_StandardInformationReview.ToList()[i].CAM_ReviewPerson == null)
                {
                    checkRev = 1;
                    break;
                }
            }
            if (checkRev == 1)
            {
                return this.JsonFaild("您选择评价人员!");
            }

            if(dto.State=="未审批" || dto.State == "未批准" || dto.State == "批准通过")
            {
                return this.JsonFaild("当前记录状态["+ dto.State + "],不允许修改保存!");
            }

            //获取小组成员
            var evaluateItem = "";
            for (int i = 0; i < dto.FL_StandardInformationReview.Count; i++)
            {
                var LawList = _coreDBContext.FL_StandardInformationReview.Where(x => x.CAM_Dept == dto.FL_StandardInformationReview.ToList()[i].CAM_Dept && x.Year == dto.years && x.CAM_Type == "评价报告").ToList();

                var NewLawLlist = new List<FL_StandardInformationReview>();
                if(LawList.Count>0)
                {
                    for (int j = 0; j < LawList.Count; j++)
                    {
                        if(_coreDBContext.FL_StandardInformation.Where(x=>x.Id==LawList[j].StandardInformationID).FirstOrDefault().CreateFactory == UserIdentity.Factory)
                        {
                            NewLawLlist.Add(LawList[j]);
                        }
                    }
                }

                for (int q = 0; q < NewLawLlist.Count; q++)
                {
                    if(NewLawLlist[q].CAM_ReviewPerson != dto.FL_StandardInformationReview.ToList()[i].CAM_ReviewPerson)
                    {
                        //修改 符合表评价人员
                        string SQLstr = @"UpDate  FL_StandardInformationReview set  CAM_ReviewPerson='" + dto.FL_StandardInformationReview.ToList()[i].CAM_ReviewPerson + "' " +
                          " where id='" + NewLawLlist[q].Id + "'";

                        SqlParameter[] param1 = new SqlParameter[] { };
                        List<SqlParameter> sqlParameterList1 = new List<SqlParameter>();
                        param1 = sqlParameterList1.ToArray();
                        try
                        {
                            var dataTable = _sQLHelper.ExecuteNonQuery(SQLstr, param1);
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                }
                //获取小组成员名单
                var PersonId = dto.FL_StandardInformationReview.ToList()[i].CAM_ReviewPerson;
                var PersonName = "";
                if (PersonId !=Guid.Empty)
                {
                     PersonName = _coreDBContext.SysUser.Where(x => x.Id == PersonId).FirstOrDefault().Name;
                }
                var DepId= dto.FL_StandardInformationReview.ToList()[i].CAM_Dept;
                var DepName = "";
                if (DepId != Guid.Empty)
                {
                     DepName = _coreDBContext.SysDepartment.Where(x => x.Id == DepId).FirstOrDefault().Name;
                }
                 evaluateItem += DepName + ": " + PersonName+"   ";
            }


            ////工厂
            dto.CreateFactory = UserIdentity.Factory;
            dto.CreateTime = System.DateTime.Now;
            dto.IsDeleted = false;
            dto.CreateUser = UserIdentity.Id;
            dto.State = "保存";
            dto.evaluateItem = evaluateItem;
            dto.auditDate = null;
            dto.approveDate = null;

            await _service.UpdateAsync(dto);

            return this.JsonSuccess(string.Empty);
        }

        /// <summary>
        /// 修改--提交审核
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("EditSaveSH")]
        [PermissionFilter(nameof(FL_EvaluationController) + "_" + nameof(FL_EvaluationController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            EditSaveSHAsync(FL_EvaluationDto dto)
        {
            int checkRev = 0;
            for (int i = 0; i < dto.FL_StandardInformationReview.Count; i++)
            {
                if (dto.FL_StandardInformationReview.ToList()[i].CAM_ReviewPerson == null)
                {
                    checkRev = 1;
                    break;
                }
            }
            if (checkRev == 1)
            {
                return this.JsonFaild("您选择评价人员!");
            }
            if(dto.State=="未审批")
            {
                return this.JsonFaild("当前记录已提交审核!");
            }
            if(dto.State=="未批准" || dto.State=="批准通过")
            {
                return this.JsonFaild("当前状态["+ dto.State + "] ,无法提交审核!");
            }
            if(dto.auditor ==null)
            {
                return this.JsonFaild("请选择指定的审批人员!");
            }
            if (dto.approver == null)
            {
                return this.JsonFaild("请选择指定的批准人员!");
            }



            ////工厂
            dto.CreateFactory = UserIdentity.Factory;
            dto.CreateTime = System.DateTime.Now;
            dto.IsDeleted = false;
            dto.CreateUser = UserIdentity.Id;
            dto.State = "未审批";
            dto.auditDate = null;
            dto.approveDate = null;

            await _service.UpdateAsync(dto);

            for (int i = 0; i < dto.FL_StandardInformationReview.Count; i++)
            {
                var LawList = _coreDBContext.FL_StandardInformationReview.Where(x => x.CAM_Dept == dto.FL_StandardInformationReview.ToList()[i].CAM_Dept && x.Year == dto.years && x.CAM_Type == "评价报告").ToList();

                var NewLawLlist = new List<FL_StandardInformationReview>();
                if (LawList.Count > 0)
                {
                    for (int j = 0; j < LawList.Count; j++)
                    {
                        if (_coreDBContext.FL_StandardInformation.Where(x => x.Id == LawList[j].StandardInformationID).FirstOrDefault().CreateFactory == UserIdentity.Factory)
                        {
                            NewLawLlist.Add(LawList[j]);
                        }
                    }
                }

                for (int q = 0; q < NewLawLlist.Count; q++)
                {
                    if (NewLawLlist[q].CAM_ReviewPerson != dto.FL_StandardInformationReview.ToList()[i].CAM_ReviewPerson)
                    {
                        //修改 符合表评价人员
                        string SQLstr = @"UpDate  FL_StandardInformationReview set  CAM_ReviewPerson='" + dto.FL_StandardInformationReview.ToList()[i].CAM_ReviewPerson + "' " +
                          " where id='" + NewLawLlist[q].Id + "'";

                        SqlParameter[] param1 = new SqlParameter[] { };
                        List<SqlParameter> sqlParameterList1 = new List<SqlParameter>();
                        param1 = sqlParameterList1.ToArray();
                        try
                        {
                            var dataTable = _sQLHelper.ExecuteNonQuery(SQLstr, param1);
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                }

            }
            //发送消息通知审批人员
            _message.SendMessage(new Toolkit.Message.Model.MessageModel()
            {
                FromUser = UserIdentity.Id,
                // ToUser = _wrkerRelationsService.GetRoleUser(_businessOptions.GKDeptName).FirstOrDefault(),
                ToUser = dto.auditor,
                Content = $"年度合规性评审在[" + DateTime.Now + "]已提交审批,请审批人员审批",
                SendTime = DateTime.Now,
                Ttitle = "年度合规性-提交审批"
            });

            return this.JsonSuccess(string.Empty);
        }




        /// <summary>
        ///  评审 
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("EditSavePS")]
        [PermissionFilter(nameof(FL_EvaluationController) + "_" + nameof(FL_EvaluationController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            EditSavePSAsync(FL_EvaluationDto dto)
        {
            var model = await _service.GetAsync(dto.Id);
            if (model.State == "未批准"  ||  model.State == "批准通过" || model.State == "未审批")
            {
                return this.JsonFaild("当前记录状态 [" + model.State + "] ,无法评审!");
            }

            for (int i = 0; i < dto.FL_StandardInformationReview.Count; i++)
            {
                var Id = dto.FL_StandardInformationReview.ToList()[i].Id;
                var CheckFit = dto.FL_StandardInformationReview.ToList()[i].FitSituation;
                var Situation = dto.FL_StandardInformationReview.ToList()[i].FitSituationOrNotDescribe;
                string SQLstr = "";
                //修改 符合表 符合标准
                if (CheckFit == "符合")
                {
                    SQLstr = @"UpDate  FL_StandardInformationReview set  FitSituation='" + Situation + "', FitSituationOrNotDescribe='' ,CAM_ReviewDate='"+ DateTime.Now+"'" +
                     " where id='" + Id + "'";
                }
                else if (CheckFit == "不符合")
                {
                    SQLstr = @"UpDate  FL_StandardInformationReview set  FitSituation='', FitSituationOrNotDescribe='" + Situation + "' ,CAM_ReviewDate='" + DateTime.Now + "'" +
                     " where id='" + Id + "'";
                }

                SqlParameter[] param1 = new SqlParameter[] { };
                List<SqlParameter> sqlParameterList1 = new List<SqlParameter>();
                param1 = sqlParameterList1.ToArray();
                try
                {
                    var dataTable = _sQLHelper.ExecuteNonQuery(SQLstr, param1);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            //获取审批人
            var Auditor = dto.auditor;

            _message.SendMessage(new Toolkit.Message.Model.MessageModel()
            {
                FromUser = UserIdentity.Id,
                // ToUser = _wrkerRelationsService.GetRoleUser(_businessOptions.GKDeptName).FirstOrDefault(),
                ToUser = Auditor,
                Content = $"年度合规性评审在[" + DateTime.Now + "]已完成评审,请查阅审批",
                SendTime = DateTime.Now,
                Ttitle = "年度合规性评审"
            });

         
            return this.JsonSuccess(string.Empty);
        }


        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(FL_EvaluationController) + "_" + nameof(FL_EvaluationController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult>
            DeleteMutiAsync([FromBody]Guid[] ids)
        {

            List<Guid> newList = new List<Guid>();
            for (int i = 0; i < ids.Count(); i++)
            {
                if (_coreDBContext.FL_Evaluation.Where(x => x.Id == ids[i]).Where(b => b.IsDeleted == false).ToList().Count > 0)
                {
                    newList.Add(ids[i]);
                }
            }
            await _service.DeleteBatchAsync(newList.ToArray());

            return this.JsonSuccess();

            //await _service.DeleteBatchAsync(ids);
            //return this.JsonSuccess();
        }


        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        public async Task<IActionResult> Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            if (model.auditor != null)
            {
                model.Mark01 = _coreDBContext.SysUser.Where(x => x.Id == model.auditor).FirstOrDefault().Name;
            }
            if (model.approver != null)
            {
                model.Mark02 = _coreDBContext.SysUser.Where(x => x.Id == model.approver).FirstOrDefault().Name;
            }
            if (model.CreateUser != null)
            {
                model.Mark03 = _coreDBContext.SysUser.Where(x => x.Id == model.CreateUser).FirstOrDefault().Name;
            }
            //var FL_StandardInformation = _coreDBContext.FL_StandardInformation.Where(x => x.CreateFactory == UserIdentity.Factory &&  x.IsDeleted==false).ToList();
            //var list = new List<FL_StandardInformation>();

            //for (int i = 0; i < FL_StandardInformation.Count; i++)
            //{
            //    var FL_StandardInformationDept1 = _coreDBContext.FL_StandardInformationDept.Where(x => x.LawRegulationsName == FL_StandardInformation[i].Id && x.DeptType == "1").ToList();
            //    var DepName = _coreDBContext.SysDepartment.Where(x => x.Id == FL_StandardInformationDept1.FirstOrDefault().Class).FirstOrDefault()?.Name;
            //    FL_StandardInformation[i].Mark03 = DepName;
            //    bool isnot = true;
            //    if (list.Count > 0)
            //    {
            //        list.ForEach(a =>
            //        {
            //            if (a.FL_StandardInformations.FirstOrDefault().Class == FL_StandardInformationDept1.FirstOrDefault().Class)
            //            {
            //                isnot = false;
            //            }
            //        });
            //    }
            //    if (isnot)
            //    {
            //        list.Add(FL_StandardInformation[i]);
            //    }
            //}

            //model.FL_StandardInformation = list;

            var retObj = new
            {
                model.Id,
                model.ReportNo,
                model.evaluateAim,
                model.evaluateRange,
                model.evaluateItem,
                model.evaluateOverview,
                model.evaluateOverview2,
                model.notFitAnalysis,
                model.evaluateResult,
                model.years,
                model.approveDate,
                model.approver,
                model.auditDate,
                model.auditor,
                model.Mark01,
                model.Mark02,
                model.Mark03, //发布人员
                model.CreateTime,
                model.State,
              //  model.FL_StandardInformation,
            };


            ////工厂
           // pagingQueryModel.Model.CreateFactory = UserIdentity.Factory;


            //IPageModel<FL_StandardInformationDto>
            //    result = await _Infservice.GetList(pagingQueryModel);



            //FL_StandardInformation.ForEach(dto =>
            //{
            //    dto.FL_StandardInformationDept1 = dto.FL_StandardInformations.Where(x => x.DeptType == "1").ToList();

            //    var str = "";
            //    dto.FL_StandardInformationDept1.ToList().ForEach(a =>
            //    {
            //        str += _coreDBContext.SysDepartment.Where(x => x.Id == a.Class).FirstOrDefault()?.Name + ",";
            //    });
            //    if (str.Length > 0)
            //        str = str.Substring(0, str.Length - 1);
            //    dto.Mark03 = str;

            //    dto.GKBM = dto.FL_StandardInformationDept1.FirstOrDefault().Class;
            //    //  dto.SysUser= dto.SysUser.Where(x=>x.Id==_coreDBContext.SysUserDuty)
            //    var Person = _coreDBContext.SysUserDuty.Where(y => y.SysDepartmentID == dto.FL_StandardInformationDept1.FirstOrDefault().Class).ToList();

            //    List<SysUserDto> ListUser = new List<SysUserDto>();
            //    for (int i = 0; i < Person.Count; i++)
            //    {
            //        var SysUser = new SysUserDto();
            //        SysUser.Id = Person[i].SysUserId;
            //        SysUser.Name = _coreDBContext.SysUser.Where(q => q.Id == Person[i].SysUserId).FirstOrDefault().Name;
            //        bool Userisnot = true;
            //        ListUser.ForEach(a =>
            //        {
            //            if (a.Name == SysUser.Name)
            //            {
            //                Userisnot = false;
            //            }
            //        });
            //        if (Userisnot)
            //        {
            //            ListUser.Add(SysUser);
            //        }
            //    }
            //    dto.SysUser = ListUser;



            //    bool isnot = true;
            //    if (list.Count > 0)
            //    {
            //        list.ForEach(a =>
            //        {
            //            if (a.Mark03 == dto.Mark03)
            //            {
            //                isnot = false;
            //            }
            //        });
            //    }
            //    if (isnot)
            //    {
            //        list.Add(dto);
            //    }
            //});

            //result.PageData = list;
            //result.RowCount = list.Count;
            //return this.JsonSuccess(result);





            return this.JsonSuccess(retObj);
        }

        /// <summary>
        /// 审核
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Examine")]
        [PermissionFilter(nameof(FL_EvaluationController) + "_" + nameof(FL_EvaluationController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            Examine(FL_EvaluationDto dto)
        {
            if(dto.State !="未审批")
            {
                return this.JsonFaild("当前状态不允许审批!");
            }
            if(dto.auditor != UserIdentity.Id)
            {
                return this.JsonFaild("您不是指定的审批人员,无法审批!");
            }
            ////工厂
            dto.CreateFactory = UserIdentity.Factory;
            dto.auditor = UserIdentity.Id;
            dto.auditDate = System.DateTime.Now;
            dto.State = "未批准";
            await _service.UpdateAsync(dto);

            _message.SendMessage(new Toolkit.Message.Model.MessageModel()
            {
                FromUser = UserIdentity.Id,
                //  ToUser = _wrkerRelationsService.GetRoleUser(_businessOptions.GKDeptName).FirstOrDefault(),
                ToUser = dto.approver,
                Content = $"年度合规性评审在[" + DateTime.Now + "]已审核,请批准",
                SendTime = DateTime.Now,
                Ttitle = "年度合规性评审-审核"
            });
            return this.JsonSuccess(string.Empty);
        }

        /// <summary>
        /// 批准
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Approval")]
        [PermissionFilter(nameof(FL_EvaluationController) + "_" + nameof(FL_EvaluationController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            Approval(FL_EvaluationDto dto)
        {
            if (dto.State != "未批准")
            {
                return this.JsonFaild("当前状态不允许批准!");
            }
            if (dto.approver != UserIdentity.Id)
            {
                return this.JsonFaild("您不是指定的批准人员,无法批准!");
            }
            ////工厂
            dto.CreateFactory = UserIdentity.Factory;
            dto.approver = UserIdentity.Id;
            dto.approveDate = System.DateTime.Now;
            dto.State = "批准通过";
            await _service.UpdateAsync(dto);

            var evaluationUserList = _coreDBContext.FL_StandardInformationReview.Where(x => x.Year == dto.years && x.CAM_Type == "评价报告").ToList();
            var NewUerList = new List<FL_StandardInformationReview>();
            for (int i = 0; i < evaluationUserList.Count; i++)
            {
                if (NewUerList.Count == 0)
                {
                    NewUerList.Add(evaluationUserList[i]);
                }
                else
                {
                    bool checkPerson = false;
                    NewUerList.ForEach(a =>
                    {
                        if (a.CAM_ReviewPerson == evaluationUserList[i].CAM_ReviewPerson)
                        {
                            checkPerson = true;
                        }
                    });
                    if (checkPerson == false)
                    {
                        NewUerList.Add(evaluationUserList[i]);
                    }

                }
            }
            //通知评价人员
            for (int j = 0; j < NewUerList.Count; j++)
            {
                var ToUserPerson = NewUerList[j].CAM_ReviewPerson;
                _message.SendMessage(new Toolkit.Message.Model.MessageModel()
                {
                    FromUser = UserIdentity.Id,
                    // ToUser = _wrkerRelationsService.GetRoleUser(_businessOptions.GKDeptName).FirstOrDefault(),
                     ToUser = ToUserPerson,
                    Content = $"年度合规性评审在[" + DateTime.Now + "]已批准,请查阅",
                    SendTime = DateTime.Now,
                    Ttitle = "年度合规性评审-批准"
                });
            }
            return this.JsonSuccess(string.Empty);
        }

        /// <summary>
        /// 退回
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("btnReturn")]
        [PermissionFilter(nameof(FL_EvaluationController) + "_" + nameof(FL_EvaluationController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            btnReturn(FL_EvaluationDto dto)
        {
            if (dto.State != "未批准" && dto.State !="未审批")
            {
                return this.JsonFaild("当前状态不是[未审批]或[未批准]状态,不允许退回!");
            }
            if(dto.State == "未批准")
            {
                if(dto.approver != UserIdentity.Id)
                {
                    return this.JsonFaild("您不是指定的批准人员,无法退回!");
                }
            }
            if (dto.State == "未审批")
            {
                if (dto.auditor != UserIdentity.Id)
                {
                    return this.JsonFaild("您不是指定的审核人员,无法退回!");
                }
            }
            var myState = "";
            if(dto.State== "未审批")
            {
                myState = "审批退回";
            }
            if (dto.State == "未批准")
            {
                myState = "批准退回";
            }


            ////工厂
            dto.CreateFactory = UserIdentity.Factory;
            dto.State = myState;
            await _service.UpdateAsync(dto);

            var evaluationUserList = _coreDBContext.FL_StandardInformationReview.Where(x => x.Year == dto.years && x.CAM_Type == "评价报告").ToList();
            var NewUerList = new List<FL_StandardInformationReview>();
            for (int i = 0; i < evaluationUserList.Count; i++)
            {
                if(NewUerList.Count==0)
                {
                    NewUerList.Add(evaluationUserList[i]);
                }
                else
                {
                    bool checkPerson = false;
                    NewUerList.ForEach(a =>
                    {
                        if(a.CAM_ReviewPerson== evaluationUserList[i].CAM_ReviewPerson)
                        {
                            checkPerson =true;
                        }
                    });
                    if(checkPerson==false)
                    {
                        NewUerList.Add(evaluationUserList[i]);
                    }

                }
            }
            //通知评价人员
            for (int j = 0; j < NewUerList.Count; j++)
            {
                var ToUserPerson = NewUerList[j].CAM_ReviewPerson;
                _message.SendMessage(new Toolkit.Message.Model.MessageModel()
                {
                    FromUser = UserIdentity.Id,
                    // ToUser = _wrkerRelationsService.GetRoleUser(_businessOptions.GKDeptName).FirstOrDefault(),
                    ToUser = ToUserPerson,
                    Content = $"年度合规性评审在[" + DateTime.Now + "]已退回,请查看",
                    SendTime = DateTime.Now,
                    Ttitle = "年度合规性评审-退回"
                });

            }
        
            return this.JsonSuccess(string.Empty);
        }


        /// <summary>
        /// 导出
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("Export")]
        [PermissionFilter(nameof(FL_EvaluationController) + "_" + nameof(FL_EvaluationController.Export), AuthorizationConst.EXPORT)]
        public async Task<IActionResult>
            Export(PagingModel<FL_EvaluationDto>
                pagingQueryModel)
        {
            var fileName = await _service.Export(pagingQueryModel);
            if (string.IsNullOrWhiteSpace(fileName))
                return this.JsonFaild(NULL_DATA_FOR_EXCEL_EXPORT);
            return this.JsonSuccess(fileName);
        }


        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="data"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost("Import")]
        //[PermissionFilter(nameof(FX_EquipmentFacilitiesController) + "_" + "导入")]
        public async Task<IActionResult> ImportAsync([FromForm]String data, [FromForm]List<IFormFile> file)
        {
            var retMeg = await _service.ImportAsync(file[0], data);
            if (!retMeg.Any())
            {
                return this.JsonSuccess();
            }
            else
            {
                return this.JsonFaild(retMeg[0].ToString());
            }
        }




        /// <summary>
        /// 获得各职能部门具体识别清单
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("GetClassList")]
        //[PermissionFilter(nameof(FL_StandardInformationWebController) + "_" + nameof(FL_StandardInformationWebController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetClassList(PagingModel<FL_StandardInformationDto>
                pagingQueryModel)
        {
            //工厂
            pagingQueryModel.Model.CreateFactory = UserIdentity.Factory;
            IPageModel<FL_StandardInformationDto> result = null;
            result = await _Infservice.GetList(pagingQueryModel);

            List<FL_StandardInformationDto> List = new List<FL_StandardInformationDto>();
            //获取归口部门

            var DepList = new List<FL_StandardInformationDto>();

            result.PageData.ForEach(dto =>
            {
                dto.FL_StandardInformationDept1 = dto.FL_StandardInformations.Where(x => x.DeptType == "1").ToList();

                if (dto.FL_StandardInformationDept1.Count > 0)
                {
                    var str = "";
                    dto.FL_StandardInformationDept1.ToList().ForEach(a =>
                    {
                        str += _coreDBContext.SysDepartment.Where(x => x.Id == a.Class).FirstOrDefault()?.Name + ",";
                    });
                    if (str.Length > 0)
                        str = str.Substring(0, str.Length - 1);
                    //获取部门名称
                    dto.Mark03 = str;
                    //获取部门ID
                    dto.GKBM = dto.FL_StandardInformationDept1.FirstOrDefault().Class;

                    bool isnot = true;
                    if (DepList.Count > 0)
                    {
                        DepList.ForEach(a =>
                        {
                            if (a.Mark03 == dto.Mark03)
                            {
                                isnot = false;
                            }
                        });
                    }
                    if (isnot)
                    {
                        DepList.Add(dto);
                    }
                }
            });

            var DeptEntity = DepList;

            var entities = _coreDBContext.FL_StandardInformation
            .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "国家法律" && x.IsDeleted == false).FirstOrDefault().Id && x.CreateFactory == UserIdentity.Factory).ToList();

            var entities_FG = _coreDBContext.FL_StandardInformation
            .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "国家法规" && x.IsDeleted == false).FirstOrDefault().Id && x.CreateFactory == UserIdentity.Factory).ToList();

            var entities_GJBM = _coreDBContext.FL_StandardInformation
            .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "国家部门规章" && x.IsDeleted == false).FirstOrDefault().Id && x.CreateFactory == UserIdentity.Factory).ToList();

            var entities_GJBMGF = _coreDBContext.FL_StandardInformation
            .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "国家部门规范" && x.IsDeleted == false).FirstOrDefault().Id && x.CreateFactory == UserIdentity.Factory).ToList();

            var entities_DFXFG = _coreDBContext.FL_StandardInformation
            .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "地方性法规及要求" && x.IsDeleted == false).FirstOrDefault().Id && x.CreateFactory == UserIdentity.Factory).ToList();

            var entities_BZHGF = _coreDBContext.FL_StandardInformation
          .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "标准和规范" && x.IsDeleted == false).FirstOrDefault().Id && x.CreateFactory == UserIdentity.Factory).ToList();

            var entities_SJGSZD = _coreDBContext.FL_StandardInformation
         .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "上级公司制度及要求" && x.IsDeleted == false).FirstOrDefault().Id && x.CreateFactory == UserIdentity.Factory).ToList();

            for (int i = 0; i < DeptEntity.ToList().Count; i++)
            {
                var NewModel = new FL_StandardInformationDto();
                NewModel.Mark01 = DeptEntity.ToList()[i].Mark03;
                //国家法律
                int count = 0;
                int SumCount = 0;
                entities.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].GKBM;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && (c.DeptType == "1" || c.DeptType == "2") && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount += count;
                NewModel.Mark02 = count.ToString();
                //国家法规
                count = 0;
                entities_FG.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].GKBM;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && (c.DeptType == "1" || c.DeptType == "2") && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount += count;
                NewModel.Mark03 = count.ToString();

                //国家部门规章
                count = 0;
                entities_GJBM.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].GKBM;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && (c.DeptType == "1" || c.DeptType == "2") && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount += count;
                NewModel.Mark04 = count.ToString();

                //国家部门规范
                count = 0;
                entities_GJBMGF.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].GKBM;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && (c.DeptType == "1" || c.DeptType == "2") && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount += count;
                NewModel.Mark05 = count.ToString();

                //地方性法规及要求
                count = 0;
                entities_DFXFG.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].GKBM;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && (c.DeptType == "1" || c.DeptType == "2") && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount += count;
                NewModel.Mark06 = count.ToString();
                //标准和规范
                count = 0;
                entities_BZHGF.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].GKBM;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && (c.DeptType == "1" || c.DeptType == "2") && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount += count;
                NewModel.Mark07 = count.ToString();

                //标准和规范
                count = 0;
                entities_SJGSZD.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].GKBM;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && (c.DeptType == "1" || c.DeptType == "2") && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount += count;
                NewModel.Mark08 = count.ToString();
                NewModel.Mark09 = SumCount.ToString();
                List.Add(NewModel);
            }
            return this.JsonSuccess(List.ToArray());
        }



        /// <summary>
        /// 获得法律法规和其他要求及更新清单
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("GetClassList2")]
        //[PermissionFilter(nameof(FL_StandardInformationWebController) + "_" + nameof(FL_StandardInformationWebController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetClassList2(PagingModel<FL_StandardInformationDto>
                pagingQueryModel)
        {
            //工厂
            pagingQueryModel.Model.CreateFactory = UserIdentity.Factory;
            IPageModel<FL_StandardInformationDto> result = null;
            result = await _Infservice.GetList(pagingQueryModel);

            List<FL_StandardInformationDto> List = new List<FL_StandardInformationDto>();
            //获取归口部门

            var DepList = new List<FL_StandardInformationDto>();

            result.PageData.ForEach(dto =>
            {
                dto.FL_StandardInformationDept1 = dto.FL_StandardInformations.Where(x => x.DeptType == "1").ToList();
                if(dto.FL_StandardInformationDept1.Count >0)
                {
                    var str = "";
                    dto.FL_StandardInformationDept1.ToList().ForEach(a =>
                    {
                        str += _coreDBContext.SysDepartment.Where(x => x.Id == a.Class).FirstOrDefault()?.Name + ",";
                    });
                    if (str.Length > 0)
                        str = str.Substring(0, str.Length - 1);
                    //获取部门名称
                    dto.Mark03 = str;
                    //获取部门ID
                    dto.GKBM = dto.FL_StandardInformationDept1.FirstOrDefault().Class;

                    bool isnot = true;
                    if (DepList.Count > 0)
                    {
                        DepList.ForEach(a =>
                        {
                            if (a.Mark03 == dto.Mark03)
                            {
                                isnot = false;
                            }
                        });
                    }
                    if (isnot)
                    {
                        DepList.Add(dto);
                    }
                }
              
            });

            var DeptEntity = DepList;

            var entities = _coreDBContext.FL_StandardInformation
             .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "国家法律" && x.IsDeleted == false).FirstOrDefault().Id && x.CreateFactory == UserIdentity.Factory).ToList();

            var entities_FG = _coreDBContext.FL_StandardInformation
            .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "国家法规" && x.IsDeleted == false).FirstOrDefault().Id && x.CreateFactory == UserIdentity.Factory).ToList();

            var entities_GJBM = _coreDBContext.FL_StandardInformation
            .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "国家部门规章" && x.IsDeleted == false).FirstOrDefault().Id && x.CreateFactory == UserIdentity.Factory).ToList();

            var entities_GJBMGF = _coreDBContext.FL_StandardInformation
            .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "国家部门规范" && x.IsDeleted == false).FirstOrDefault().Id && x.CreateFactory == UserIdentity.Factory).ToList();

            var entities_DFXFG = _coreDBContext.FL_StandardInformation
            .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "地方性法规及要求" && x.IsDeleted == false).FirstOrDefault().Id && x.CreateFactory == UserIdentity.Factory).ToList();

            var entities_BZHGF = _coreDBContext.FL_StandardInformation
          .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "标准和规范" && x.IsDeleted == false).FirstOrDefault().Id && x.CreateFactory == UserIdentity.Factory).ToList();

            var entities_SJGSZD = _coreDBContext.FL_StandardInformation
         .Where(x => x.FileLeve == _coreDBContext.FL_FileLevel.Where(b => b.FileLeve == "上级公司制度及要求" && x.IsDeleted == false).FirstOrDefault().Id && x.CreateFactory == UserIdentity.Factory).ToList();

            var NewModel = new FL_StandardInformationDto();
            int SumCount_GJFL = 0;
            int SumCount_GJFG = 0;
            int SumCount_GJBMGZ = 0;
            int SumCount_GJBMGF = 0;
            int SumCount_DFXFGJYQ = 0;
            int SumCount_BZHGF = 0;
            int SumCount_GSZDJYQ = 0;

            for (int i = 0; i < DeptEntity.ToList().Count; i++)
            {
                NewModel.Mark01 = DeptEntity.ToList()[i].Mark03;
                //国家法律
                int count = 0;

                entities.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].GKBM;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && c.DeptType == "1" && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount_GJFL += count;
                NewModel.Mark02 = count.ToString();
                //国家法规
                count = 0;
                entities_FG.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].GKBM;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && c.DeptType == "1" && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount_GJFG += count;
                NewModel.Mark03 = count.ToString();

                //国家部门规章
                count = 0;
                entities_GJBM.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].GKBM;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && c.DeptType == "1" && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount_GJBMGZ += count;
                NewModel.Mark04 = count.ToString();

                //国家部门规范
                count = 0;
                entities_GJBMGF.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].GKBM;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && c.DeptType == "1" && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount_GJBMGF += count;
                NewModel.Mark05 = count.ToString();

                //地方性法规及要求
                count = 0;
                entities_DFXFG.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].GKBM;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && c.DeptType == "1" && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount_DFXFGJYQ += count;
                NewModel.Mark06 = count.ToString();
                //标准和规范
                count = 0;
                entities_BZHGF.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].GKBM;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && c.DeptType == "1" && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount_BZHGF += count;
                NewModel.Mark07 = count.ToString();

                //上级公司制度及要求
                count = 0;
                entities_SJGSZD.ForEach(x =>
                {
                    var DepId = DeptEntity.ToList()[i].GKBM;
                    if (_coreDBContext.FL_StandardInformationDept.Where(a => a.Class == DepId).ToList().Count > 0)
                    {
                        var NewEntity = _coreDBContext.FL_StandardInformationDept.Where(c => c.LawRegulationsName == x.Id && c.DeptType == "1" && c.Class == DepId).ToList();
                        if (NewEntity.Count > 0)
                        {
                            count++;
                        }
                    }
                });
                SumCount_GSZDJYQ += count;
                NewModel.Mark08 = count.ToString();
                //  NewModel.Mark09 = SumCount.ToString();
            }
            NewModel.SumCount_GJFL = SumCount_GJFL.ToString();
            NewModel.SumCount_GJFG = SumCount_GJFG.ToString();
            NewModel.SumCount_GJBMGZ = SumCount_GJBMGZ.ToString();
            NewModel.SumCount_GJBMGF = SumCount_GJBMGF.ToString();
            NewModel.SumCount_DFXFGJYQ = SumCount_DFXFGJYQ.ToString();
            NewModel.SumCount_BZHGF = SumCount_BZHGF.ToString();
            NewModel.SumCount_GSZDJYQ = SumCount_GSZDJYQ.ToString();
            int SumTotal = SumCount_GJFL + SumCount_GJFG +
                SumCount_GJBMGZ + SumCount_GJBMGF +
                SumCount_DFXFGJYQ + SumCount_BZHGF +
                SumCount_GSZDJYQ;
            NewModel.Mark10 = SumTotal.ToString();
            List.Add(NewModel);
            return this.JsonSuccess(List.ToArray());
        }
        #endregion
    }


}

